﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PassBag.Data;
using PassBag.Logic.Settings;
using PassBag.Logic;
using PassBag.Logic.Backup;
using PassBag.Presentation.Backup;
using PassBag.Logic.Resources;

namespace PassBag.Presentation {
    public partial class SettingDialog : Form {
        private bool _settingsLoaded = false;

        private enum SettingTabPage {
            MainTab = 0,
            PasswordPolicy = 1,
            Backup = 2
        };

        public SettingDialog () {
            InitializeComponent ();

            LoadRessource ();

            SelectTabPage (SettingTabPage.MainTab);

            LoadLanguageList ();

            LoadSettings ();
        }

        #region Load Resource

        /// <summary>
        /// Loads the ressource.
        /// </summary>
        private void LoadRessource () {
            this.Text = Properties.Resources.SettingDialog_Text;
            this.mainButton.Text = Properties.Resources.SettingDialog_ToolBarMain;
            this.passPolicyButton.Text = Properties.Resources.SettingDialog_ToolBarPass;
            this.backupButton.Text = Properties.Resources.SettingDialog_ToolBarBackup;

            this.label1.Text = Properties.Resources.SettingDialog_LockAfterStart;
            this.label2.Text = Properties.Resources.SettingDialog_LockAfterEnd;

            this.generalLockOnMinimizeCheckBox.Text = Properties.Resources.SettingDialog_LockMinimizeCheck;
            this.generalClearClipboardOnLockCheckBox.Text = Properties.Resources.SettingDialog_ClearMinimizeCheck;
            this.label9.Text = Properties.Resources.SettingDialog_LanguageCombo;

            this.passPolicyUseDefaultCheckBox.Text = Properties.Resources.SettingDialog_DefaultPassCheck;
            this.label3.Text = Properties.Resources.SettingDialog_NbDiditText;
            this.label4.Text = Properties.Resources.SettingDialog_NbSpecialCharText;
            this.label5.Text = Properties.Resources.SettingDialog_MaxPassLength;

            this.label6.Text = Properties.Resources.SettingDialog_LinuxPathText;
            this.label7.Text = Properties.Resources.SettingDialog_OSXPathText;
            this.label8.Text = Properties.Resources.SettingDialog_WinPathText;

            this.okBtn.Text = Properties.Resources.Global_OkButton;
            this.cancelBtn.Text = Properties.Resources.Global_CancelButton;

        }

        #endregion


        private void LoadLanguageList () {
            generalLanguageComboBox.Items.Clear ();
            generalLanguageComboBox.Items.AddRange (LanguageList.GetLanguageList());
        }

        private void LoadSettings () {
            generalUseSystemTrayCheckBox.Checked = XmlSettingsManager.Settings.GeneralUseSystemTray;
            generalLockAfterXSecondNumericUpDown.Value = XmlSettingsManager.Settings.GeneralLockAfterXSecond;
            generalLockOnMinimizeCheckBox.Checked = XmlSettingsManager.Settings.GeneralLockOnMinimize;
            generalClearClipboardOnLockCheckBox.Checked = XmlSettingsManager.Settings.GeneralClearClipboardOnLock;
            generalStartAtLoginCheckBox.Checked = XmlSettingsManager.Settings.GeneralStartAtLogin;
            generalLanguageComboBox.SelectedIndex = FindLanguageItemByKey(XmlSettingsManager.Settings.GeneralLanguage);
            backupDefaultOSXTextBox.Text = XmlSettingsManager.Settings.BackupDefaultOSX;
            backupDefaultLinuxTextBox.Text = XmlSettingsManager.Settings.BackupDefaultLinux;
            backupDefaultWindowsTextBox.Text = XmlSettingsManager.Settings.BackupDefaultWindows;
            passPolicyUseDefaultCheckBox.Checked = XmlSettingsManager.Settings.PassPolicyUseDefault;
            passPolicyNumberOfDigitNumericUpDown.Value = XmlSettingsManager.Settings.PassPolicyNumberOfDigit;
            passPolicyNumberOfSpecialCharsNumericUpDown.Value = XmlSettingsManager.Settings.PassPolicyNumberOfSpecialChars;
            passPolicyMaxLengthNumericUpDown.Value = XmlSettingsManager.Settings.PassPolicyMaxLength;

            _settingsLoaded = true;
        }

        private int FindLanguageItemByKey (string key) {
            int index = 0;

            foreach (Language item in generalLanguageComboBox.Items) {
                if (item.LanguageKey.Equals(key)) {
                    break;
                }
                index++;
            }

            return index;
        }

        private void SaveSettings () {
            XmlSettingsManager.Settings.GeneralUseSystemTray = generalUseSystemTrayCheckBox.Checked;
            XmlSettingsManager.Settings.GeneralLockAfterXSecond = Convert.ToInt32(generalLockAfterXSecondNumericUpDown.Value);
            XmlSettingsManager.Settings.GeneralLockOnMinimize = generalLockOnMinimizeCheckBox.Checked;
            XmlSettingsManager.Settings.GeneralClearClipboardOnLock = generalClearClipboardOnLockCheckBox.Checked;
            XmlSettingsManager.Settings.GeneralStartAtLogin = generalStartAtLoginCheckBox.Checked;
            XmlSettingsManager.Settings.GeneralLanguage = ((Language)generalLanguageComboBox.SelectedItem).LanguageKey;
            
            XmlSettingsManager.Settings.BackupDefaultOSX = backupDefaultOSXTextBox.Text;
            XmlSettingsManager.Settings.BackupDefaultLinux = backupDefaultLinuxTextBox.Text;
            XmlSettingsManager.Settings.BackupDefaultWindows = backupDefaultWindowsTextBox.Text;
            XmlSettingsManager.Settings.PassPolicyUseDefault = passPolicyUseDefaultCheckBox.Checked;
            XmlSettingsManager.Settings.PassPolicyNumberOfDigit = Convert.ToInt32(passPolicyNumberOfDigitNumericUpDown.Value);
            XmlSettingsManager.Settings.PassPolicyNumberOfSpecialChars = Convert.ToInt32(passPolicyNumberOfSpecialCharsNumericUpDown.Value);
            XmlSettingsManager.Settings.PassPolicyMaxLength = Convert.ToInt32(passPolicyMaxLengthNumericUpDown.Value);

            XmlSettingsManager.Save ();
        }

        private void SelectTabPage (SettingTabPage tabPage) {
            settingsTabControl.SelectedTab = settingsTabControl.TabPages[(int)tabPage];
            SelectButton (tabPage);
        }

        private void SelectButton (SettingTabPage tabPage) {
            switch (tabPage) {
                case SettingTabPage.MainTab:
                    mainButton.Checked = true;
                    passPolicyButton.Checked = false;
                    backupButton.Checked = false;
                    break;
                case SettingTabPage.PasswordPolicy:
                    mainButton.Checked = false;
                    passPolicyButton.Checked = true;
                    backupButton.Checked = false;
                    break;
                case SettingTabPage.Backup:
                    mainButton.Checked = false;
                    passPolicyButton.Checked = false;
                    backupButton.Checked = true;
                    break;
            }
        }

        private void mainButton_CheckedChanged (object sender, EventArgs e) {
            
        }

        private void passPolicyButton_CheckedChanged (object sender, EventArgs e) {
            
        }

        private void backupButton_CheckedChanged (object sender, EventArgs e) {
            
        }

        private void mainButton_Click (object sender, EventArgs e) {
            SelectTabPage (SettingTabPage.MainTab);
        }

        private void passPolicyButton_Click (object sender, EventArgs e) {
            SelectTabPage (SettingTabPage.PasswordPolicy);
        }

        private void backupButton_Click (object sender, EventArgs e) {
            SelectTabPage (SettingTabPage.Backup);
        }

        private void okBtn_Click (object sender, EventArgs e) {
            SaveSettings ();
            DialogResult = DialogResult.OK;
            Close ();
 

        }

        private void backupDefaultLinuxButton_Click (object sender, EventArgs e) {
            object file = BackupSetFileDialog.SelectFile (Properties.Resources.SettingDialog_SelectPathDialog);

            if (!file.Equals (DialogResult.Cancel))
                backupDefaultLinuxTextBox.Text = file.ToString();
        }

        private void backupDefaultOSXButton_Click (object sender, EventArgs e) {
            object file = BackupSetFileDialog.SelectFile (Properties.Resources.SettingDialog_SelectPathDialog);

            if (!file.Equals (DialogResult.Cancel))
                backupDefaultOSXTextBox.Text = file.ToString ();
        }

        private void backupDefaultWindowsButton_Click (object sender, EventArgs e) {
            object file = BackupSetFileDialog.SelectFile (Properties.Resources.SettingDialog_SelectPathDialog);

            if (!file.Equals (DialogResult.Cancel))
                backupDefaultWindowsTextBox.Text = file.ToString ();
        }

        private void passPolicyNumberOfDigitNumericUpDown_ValueChanged (object sender, EventArgs e) {
            UpdateMaxPasswordLength ();
        }

        private void passPolicyNumberOfSpecialCharsNumericUpDown_ValueChanged (object sender, EventArgs e) {
            UpdateMaxPasswordLength ();
        }

        private void UpdateMaxPasswordLength () {
            if (_settingsLoaded) {
                int noAlphaLength = Convert.ToInt32 (passPolicyNumberOfDigitNumericUpDown.Value) + Convert.ToInt32 (passPolicyNumberOfSpecialCharsNumericUpDown.Value);

                if (Convert.ToInt32 (passPolicyNumberOfSpecialCharsNumericUpDown.Value) < (noAlphaLength + PasswordPolicy.MIN_CHARS)) {
                    passPolicyNumberOfSpecialCharsNumericUpDown.Value = (noAlphaLength + PasswordPolicy.MIN_CHARS);
                }
                
            }
        }


    }
}
